package com.palm.hubo2o.view;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Checkable;
import android.widget.CheckedTextView;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.palm.hubo2o.R;
import com.palm.palmutils.LayoutUtils;

import java.util.Locale;

import butterknife.Bind;
import butterknife.BindColor;
import butterknife.ButterKnife;

/**
 * Created by dest1 on 2015/10/14.
 */
public class K3BallView extends FrameLayout implements Checkable {
    @Bind(R.id.ball_num)
    CheckedTextView tvNum;
    @Bind(R.id.ball_award)
    CheckedTextView tvAward;
    @Bind(R.id.ball_omit)
    TextView tvOmit;
    @Bind(R.id.ball_layout)
    View mLayout;
    @BindColor(R.color.yellow)
    int yellow;
    @BindColor(R.color.colorK3Background)
    int light;
    @BindColor(R.color.colorK3BackgroundDark)
    int dark;
    private int mPadding;
    private boolean mChecked;

    private GradientDrawable norm;
    private GradientDrawable checked;

    public K3BallView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    public K3BallView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public K3BallView(Context context) {
        this(context, null);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.widget_k3_ballview, this);
        ButterKnife.bind(this);
        mPadding = LayoutUtils.dp2pix(context, 5);
        mLayout.setPadding(mPadding * 2, mPadding * 2, mPadding * 2, mPadding * 2);
        int[][] numstates = new int[2][];
        numstates[0] = new int[]{android.R.attr.state_checked};
        numstates[1] = new int[]{};
        tvNum.setTextColor(new ColorStateList(numstates, new int[]{Color.YELLOW, Color.WHITE}));
        int[][] awardstates = new int[2][];
        awardstates[0] = new int[]{android.R.attr.state_checked};
        awardstates[1] = new int[]{};
        tvAward.setTextColor(new ColorStateList(awardstates, new int[]{Color.YELLOW, Color.WHITE}));
        tvAward.setAlpha(0.8f);
        int strokeWidth = LayoutUtils.dp2pix(getContext(), 1.5f);
        //普通状态
        norm = new GradientDrawable();
//        norm.setShape(GradientDrawable.RADIAL_GRADIENT);
        norm.setCornerRadius(5);
        norm.setColor(light);
        norm.setStroke(strokeWidth, dark);
        //选中状态
        checked = new GradientDrawable();
//        checked.setShape(GradientDrawable.RECTANGLE);
        checked.setCornerRadius(5);
        checked.setColor(light);
        checked.setStroke(strokeWidth, yellow);
        updateCheckState();
    }


    @Override
    public void setChecked(boolean checked) {
        if (mChecked != checked) {
            mChecked = checked;
            updateCheckState();

        }


    }

    private void updateCheckState() {
        tvNum.setChecked(mChecked);
        tvAward.setChecked(mChecked);
        if (mChecked) {
            mLayout.setBackgroundDrawable(checked);
        } else {
            mLayout.setBackgroundDrawable(norm);
        }
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }

    public void setNumText(String numberText) {
        tvNum.setText(numberText);
    }

    public String getNumTex() {
        return tvNum.getText().toString();
    }

    public void setAward(int award) {
        if (!tvAward.isShown()) {
            tvAward.setVisibility(View.VISIBLE);
            mLayout.setPadding(mPadding, mPadding, mPadding, mPadding);
        }
        tvAward.setText(String.format(Locale.getDefault(), "奖金%d", award));
    }

    public void setAward(String award) {
        if (!tvAward.isShown()) {
            tvAward.setVisibility(View.VISIBLE);
            mLayout.setPadding(mPadding, mPadding, mPadding, mPadding);
        }
        tvAward.setText(award);
    }

    public void showInfo(boolean show) {
//        if (!(tvOmit.isShown() == show)) {
//            tvOmit.setVisibility(show ? View.VISIBLE : View.GONE);
//        }
        tvOmit.setVisibility(show ? View.VISIBLE : View.GONE);
        tvAward.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    public void setOmit(String omit) {
        if (!tvOmit.isShown())
            tvOmit.setVisibility(VISIBLE);
        tvOmit.setText(omit);
    }
}
